package gui;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import depot.NoExistParcelException;

import thread.CustomerThread;
import thread.WorkThread;
import mvc.Logger;
import mvc.Observer;
import mvc.ObserverManager;


public class WorkerGUI extends JFrame implements Observer, ActionListener{
	
	//south panel
	private JButton process;
	private JButton updatebutton;
	private JTextField updatefield;
	private JTextArea showwork = new JTextArea(5,10);
	private JMenuItem importItem;
	private JMenuItem exit;
	private JMenuItem report;
	private JMenuItem stop;
	private ObserverManager oManager;
	
	private Thread[] thread = new Thread[2];
	
	/**
	 * The constructor of WorkerGUI, read input files. 
	 * @param oManager ObserverManager includes all the needed method to collect parcel
	 */
	public WorkerGUI(ObserverManager oManager) {
		this.oManager = oManager;
		oManager.registerObserver(this);
		
		setLayout(new BorderLayout(5,5));
	    setSize(800,600);
		setLocation(350,100);
		this.setTitle("Depot");
		try {
			oManager.readFile("Customer");
			oManager.readFile("Parcel");
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		add(BorderLayout.SOUTH, setSouthPanel());
		add(BorderLayout.NORTH, setMenu());
		
		add(new CustomerParcel_Panel(oManager),BorderLayout.CENTER);
		
		thread[0] = new Thread(new WorkThread(0,oManager));
		thread[1] = new Thread(new WorkThread(1,oManager));
		setVisible(true);
	}
	
	/**
	 * this panel is to show the receipt of parcels of every worker deal with , and it also includes start
	 * process collection. 
	 * @return workerPanel it shows the receipt of parcels
	 */
	public JPanel setWorkerPanel(){
		JPanel workerPanel = new JPanel(new BorderLayout(5,5));
		
		JPanel panel = new JPanel(new GridLayout(1,1));
		process = new JButton("Process");
		process.addActionListener(this);
		panel.add(process);
		
		
		showwork.setEditable(false);
		JScrollPane scrollList = new JScrollPane(showwork);
		workerPanel.add(panel,BorderLayout.NORTH);
		workerPanel.add(scrollList,BorderLayout.CENTER);
		
		return workerPanel;
	}
	
	/**
	 * this method is to build panel to do update the speed of running time
	 * @return updatePanel it includes demanded changed speed of running time
	 */
	public JPanel setUpdatePanel(){
		JPanel updatePanel= new JPanel(new GridLayout(1,2));
		updatebutton = new JButton("Update");
		updatefield = new JTextField();
		
		updatePanel.add(updatefield);
		updatePanel.add(updatebutton);
		return updatePanel;
	}
	
	/**
	 * this method is to put setUpPanel and setWorkerPanel into the same panel
	 * @return southPanel it includes all the south panel
	 */
	public JPanel setSouthPanel(){
		JPanel southPanel = new JPanel(new BorderLayout(5,5));
		southPanel.add(setWorkerPanel(),BorderLayout.NORTH);
		southPanel.add(setUpdatePanel(),BorderLayout.CENTER);
		
		return southPanel;
	}
	
	/**
	 * this method is to build a menu to do some functions
	 * @return menu it includes some menu items to do the functions
	 */
	public JMenuBar setMenu(){
		JMenuBar menu= new JMenuBar();
		JMenu function = new JMenu("Function");
		importItem = new JMenuItem("Import");
		importItem.addActionListener(this);
		exit = new JMenuItem("Exit");
		exit.addActionListener(this);
		report = new JMenuItem("Report");
		report.addActionListener(this);
		stop = new JMenuItem("Stop");
		stop.addActionListener(this);
		function.add(importItem);
		function.add(report);
		function.add(stop);
		function.add(exit);
		
		menu.add(function);
		return menu;
	}
	
	/**
	 *this method is to change obtained text from updatefield  into integer type
	 * @return speed the user want the speed to run collection
	 */
	public int getSpeed_text()
	{	if(!this.updatefield.getText().isEmpty())
			return Integer.parseInt(this.updatefield.getText());
		else
			return 1000;
	
	}
	
	
	/**
	 * this method is to add listener to update button in order to let update button could 
	 * response related action
	 * @param a1 it stands for ActionListener
	 */
	public void addSetListener (ActionListener a1) {
		updatebutton.addActionListener(a1);
	}
	

	/**
	 * this method is to obtain receipt from observer manager
	 */

	public void update() {		
		String text = oManager.getReceipt();
		if(text != null){
		this.showwork.setText(text);
		}
	}
	
	

	/**
	 * this method is to respond button clicked and menu item clicked, and it call the methods 
	 * from observer manager and logger.
	 */
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() ==process){
			
			Logger.logreport("Collection processing has started!");
			for(int i=0; i<2; i++){
				thread[i].start();
				
				try {
					Thread.sleep(1);
				} catch (InterruptedException e1) {
					e1.printStackTrace();
				}
				
			}
			
		}else if(e.getSource() == this.importItem){
			
			Logger.logreport("Add new Customers!");
			new CustomerThread(oManager).start();
			
		}else if(e.getSource() == this.report){
			
			Logger.logreport("Output the report!");
			try {
				oManager.writeToFile("Result");
			} 
			catch (NoExistParcelException nepe) {
				
				nepe.printStackTrace();
				
			}
		}
		else if(e.getSource() == this.exit){
			Logger.logreport("Exit the process!");
			int option = JOptionPane.showConfirmDialog(null, "Do you want to exit?", "Exit",JOptionPane.YES_OPTION);
			 if(option == JOptionPane.YES_NO_OPTION)
				 System.exit(0);
		}
		
		else if(e.getSource() == this.stop){
			
			Logger.logreport("Stop the thread!");
			oManager.setState(false);
			
		}
		
	}
}

